<?php

class Ads_Model_Rentdetails {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Ads_Model_DbTable_Rentdetails();
    }

    public function fetchAll() {
        $select = $this->_dbtable->select();
        $select->where('banned = false');
        return $this->_dbtable->fetchAll($select);
    }

    public function fetchFull() {
        $select = $this->_dbtable->select();
        return $this->_dbtable->fetchAll($select);
    }

    public function getRentDetailsBySr($sr) {
        $select = $this->_dbtable->select();
        $select->where('id = ? ', $sr);
        return $this->_dbtable->fetchAll($select);
    }

    public function getRentDetailsByUserId($userId) {
        $select = $this->_dbtable->select();
        $select->where('user_id  = ?', $userId);
        return $this->_dbtable->fetchAll($select);
    }

    public function createNewProperty($data) {
        $id = $this->_dbtable->insert($data);
        return $id;
    }

    public function updatePropertyDetails($data, $id) {
        $this->_dbtable->update($data, "id = $id");
    }

    public function getDetailsJson($fullDetail) {
        $djson = json_decode($fullDetail);
        return $djson->details;
    }

    public function getPhotoArrayFromJson($fullDetails) {
        $photoArray = array();
        if (sizeof($fullDetails->photo) > 0) {
            foreach ($fullDetails->photo as $p) {
                array_push($photoArray, $p);
            }
        }
        return $photoArray;
    }

    public function isBlocked($propertyId) {
        $data = $this->getRentDetailsBySr($propertyId);
        return $data[0]['banned'];
    }

    public function getCityIdOfPropertyJson($details) {
        return $details->details->city;
    }

    public function getStateIdofPropertyJson($details) {
        return $details->details->state;
    }

    public function getZipIdOfPropertyJson($details) {
        return $details->details->zipcode;
    }

    public function getProprtyTypeIdofPropertyJson($details) {
        return $details->details->propertyType;
    }

    public function getRentalPeriodIdOfPropertyJson($details) {
        return $details->details->rentalPeriod;
    }

    public function getFurnishTypeIdOfPropertyJson($details) {
        return $details->details->furnishType;
    }

    public function getRoomOfPropertyJson($details) {
        return $details->details->rooms;
    }

    public function getAreaOfPropertyJson($details) {
        return $details->details->area;
    }

    public function getRentOfPropertyJson($details) {
        return $details->details->rent;
    }

    public function getRentalPeriod($details) {
        $db = new Ads_Model_Rentsetting();
        $rentalPeriodArray = $db->getRentalPeriodArray();
        return $rentalPeriodArray->rental_period[$details->rentalPeriod];
    }

    public function getPropertyType($details) {
        $db = new Ads_Model_Rentsetting();
        $propertyTypeArray = $db->getProprertyTypeArray();
        return $propertyTypeArray->property_type[$details->propertyType];
    }

    public function getFurnishType($details) {
        $db = new Ads_Model_Rentsetting();
        $furnishArray = $db->getFurnishTypeArray();
        return $furnishArray->furnish_type[$details->furnishType];
    }

    public function getDepositeType($details) {
        $db = new Ads_Model_Rentsetting();
        $depositeArray = $db->getDepositeTypeArray();
        return $depositeArray->deposite_type[$details->depositeSelect];
    }

    public function getPrerentType($details) {
        $db = new Ads_Model_Rentsetting();
        $prerentArray = $db->getPrerentTypeArray();
        return $prerentArray->deposite_type[$details->prerentSelect];
    }

    public function todayNew() {
        $select = $this->_dbtable->select();
        $select->where("date = ?", date('Y-m-d', date('U')));
        return $this->_dbtable->fetchAll($select);
    }

    public function deleteProperty($id) {
        $row = $this->_dbtable->fetchRow("id = $id");
        $row->delete();
    }

    public function getFormatedRentDetailsByPropertyId($propertyId) {
        $rentDetailsBySr = $this->getRentDetailsBySr($propertyId);
        return $this->getDetailsJson($rentDetailsBySr[0]['details']);
    }

}

